package com.sunrise.xdoc.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Repository;

import com.sunrise.xdoc.entity.order.DonHang;

@Repository("donHangDao")
public class DonHangDao extends BaseDao<DonHang> {

	@Override
	public List<DonHang> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) {
		// Eager load chiTietDonHangs
		// Nếu để Eager load ở entity, thì findByCriteria firstResult,
		// maxResult không đúng.
		// Lý do: eager load --> join table DonHang và ChiTietDonHang --> kết
		// quả ra nhiều rows thông tin đơn hàng trùng nhau
		// Khắc phục: lazy load ChiTietDonHang, eager load bằng cách
		// override findByCriteria(DetachedCriteria criteria, int firstResult,
		// int maxResults).
		List<DonHang> donHangs = super.findByCriteria(criteria, firstResult, maxResults);
		for (DonHang dh : donHangs) {
			dh.getChiTietDonHangs().size(); // Call to eager load
		}
		return donHangs;
	}

}
